package cn.edu.gcu.giftems.service;

import cn.edu.gcu.giftems.daos.OrderItemDao;
import cn.edu.gcu.giftems.pojos.OrderItem;
import cn.edu.gcu.giftems.utils.PageUtils;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * TODO: Description here...
 *
 * @author ZhengHQ
 * @create 2018-09-24
 * @see
 */
@Transactional
public class OrderItemService {

    private OrderItemDao orderItemDao = new OrderItemDao();

    public OrderItemDao getOrderItemDao() {
        return orderItemDao;
    }

    public void setOrderItemDao(OrderItemDao orderItemDao) {
        this.orderItemDao = orderItemDao;
    }

    public void save(OrderItem orderItem) {
        orderItemDao.save(orderItem);
    }

    //根据订单单项编号查询订单单项信息
    public OrderItem findById(int id) {
        return orderItemDao.finById(id);
    }

    //更新订单单项信息
    public void update(OrderItem orderItem) {
        orderItemDao.update(orderItem);
    }

    //根据订单单项id删除订单单项
    public void delete(OrderItem orderItem) {
        orderItemDao.delete(orderItem);
    }

    public int findCount() {
        return orderItemDao.findCount();
    }


    //分页查询
    public PageUtils<OrderItem> findByPage(int currPage) {
        PageUtils<OrderItem> PageUtils = new PageUtils<OrderItem>();
        //封装当前的页数
        PageUtils.setCurrPage(currPage);
        //封装每页记录数
        int pageSize = 10;
        PageUtils.setPageSize(pageSize);
        //封装总记录数
        int totalCount = orderItemDao.findCount();
        PageUtils.setTotalCount(totalCount);
        //封装总页数
        int totalPage = totalCount%pageSize == 0?totalCount/pageSize:(totalCount/pageSize+1);
        PageUtils.setTotalPage(totalPage);
        //封装每页显示的数据
        int begin = (currPage - 1)*pageSize;
        List<OrderItem> list = orderItemDao.findByPage(begin,pageSize);
        PageUtils.setList(list);
        return PageUtils;
    }

    public List<OrderItem[]> rank() {
        return orderItemDao.rank();
    }
}
